package com.fh.fruit.controller.count;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.fruit.service.bill.BillManager;
import com.fh.fruit.service.count.CountManager;
import com.fh.fruit.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "EP")
public class EPController extends BaseController {
	String menuUrl = "EP/listCount.do"; //菜单地址(权限用)
	
    @Resource(name = "CountManager")
    private CountManager countManager;


    /**账单列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listCount")
	public ModelAndView listCount(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		page.setPd(pd);
		List<Map<String,Object>> sanNongGoodsList = countManager.SanNongGoodsCount(page);
		List<Map<String, Object>> allCount = countManager.SanNongAllCount(page);
		DecimalFormat count = new DecimalFormat("0.00");
		String countStr = "0.00";
		if(allCount.isEmpty()){
			countStr = "0.00";
		}else{
			for(int i = 0;i<allCount.size();i++){
				if(allCount.get(i) == null){
					countStr = "0.00";
				}else{
					Map<String, Object> bOrder = allCount.get(i);
					countStr = count.format(bOrder.get("allCount")) ;
				}
			}
		}
		//三农粮票统计
		List<Map<String,Object>> sanNongTickerList = countManager.SanNongTickerCount(page);
		String tickercount = "0.00";//粮票
		if(sanNongTickerList.isEmpty()){
			tickercount = "0.00";
		}else{
			for(int i = 0;i<sanNongTickerList.size();i++){
				if(sanNongTickerList.get(i) == null){
					tickercount = "0.00";
				}else{
					Map<String, Object> bOrder = sanNongTickerList.get(i);
					tickercount = count.format(bOrder.get("ticket"));
				}
			}
		}
		//三农其他统计
		List<Map<String,Object>> sanNongOtherList = countManager.SanNongOtherCount(page);
		String directPush = "0.00";//直推
		String push = "0.00";//间推
		String seePoint = "0.00";//见点
		String managementFee = "0.00";//管理费
		String reinvestment = "0.00";//复投
		if(sanNongOtherList.isEmpty()){
			
		}else{
			for(int i=0;i<sanNongOtherList.size();i++){
				Map<String,Object> resMap = sanNongOtherList.get(i);
				if(resMap.get("type").equals("00")){//消费（00管理费，01复投）
					if(resMap.get("sub_type").equals("00")){//管理费
						managementFee = count.format(resMap.get("price"));
					}else if(resMap.get("sub_type").equals("01")){//复投
						reinvestment = count.format(resMap.get("price"));
					}
				}else if(resMap.get("type").equals("02")){//奖金（ 01 直推奖 02 间推奖  03 见点奖）
					if(resMap.get("sub_type").equals("01")){//直推奖
						directPush = count.format(resMap.get("price"));
					}else if(resMap.get("sub_type").equals("02")){// 间推奖
						push = count.format(resMap.get("price"));
					}else if(resMap.get("sub_type").equals("03")){//见点奖
						seePoint = count.format(resMap.get("price"));
					}
				}
			}
		}
		Double platform = Double.parseDouble(countStr) - Double.parseDouble(tickercount) - 
							Double.parseDouble(managementFee) - Double.parseDouble(reinvestment) - 
							Double.parseDouble(directPush) - Double.parseDouble(push)-
							Double.parseDouble(seePoint);
		mv.addObject("sanNongGoodsList", sanNongGoodsList);
		mv.addObject("count", countStr);
		mv.addObject("tickercount", tickercount);
		mv.addObject("managementFee", managementFee);
		mv.addObject("reinvestment", reinvestment);
		mv.addObject("directPush", directPush);
		mv.addObject("push", push);
		mv.addObject("seePoint", seePoint);
		mv.addObject("platform", platform);
		mv.setViewName("farm/count/sanNontCount");
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	/**
	 * 销售明细
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param page
	* @param httpSession
	* @return
	* @throws Exception
	 */
	@RequestMapping(value="/getSanNongSaleslist")
	public ModelAndView getSanNongSaleslist(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData(); 
		page.setPd(pd);
		DecimalFormat countDec = new DecimalFormat("0.00");
		DecimalFormat countNum = new DecimalFormat("0");
		List<Map<String,Object>> salesList = countManager.getSanNongSaleslistPage(page);//销售明细
		List<Map<String, Object>> count = countManager.SanNongGoodsSalesCount(page);//销售明细统计
		String num = null;
		String price = null;
		if(count.isEmpty()){
			num = "0";
			price = "0.00";
		}else{
			for(int i = 0; i < count.size(); i++){
				if(count.get(i) == null){
					num = "0";
					price = "0.00";
				}else{
					num = countNum.format(count.get(i).get("num"));
					price = countDec.format(count.get(i).get("price"));
				}
			}
		}
		mv.addObject("salesList", salesList);
		mv.addObject("num", num);
		mv.addObject("price", price);
		mv.setViewName("farm/count/sales_list");
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	/**
	 * 奖励明细
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param page
	* @param httpSession
	* @return
	* @throws Exception
	 */
	@RequestMapping(value="/getSanNongRewardlistPage")
	public ModelAndView getSanNongRewardlistPage(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData(); 
		String sub_type=pd.getString("subType");
		pd.put("sub_type",sub_type); 
		page.setPd(pd);
		DecimalFormat countDec = new DecimalFormat("0.00");
		DecimalFormat countNum = new DecimalFormat("0");
		String rewardType = pd.getString("rewardType");
		if(StringUtils.isNotBlank(rewardType)){
			if(rewardType.equals("1")){
				List<Map<String,Object>> rewardList = countManager.getSanNongRewardTickerlistPage(page);//粮票奖励
				List<Map<String, Object>> count = countManager.SanNongTickerCount(page);//粮票奖励统计
				String price = null;
				if(count.isEmpty()){
					price = "0.00";
				}else{
					for(int i = 0; i < count.size(); i++){
						if(count.get(i) == null){
							price = "0.00";
						}else{
							price = countDec.format(count.get(i).get("allCount"));
						}
					}
				}
				mv.addObject("rewardList", rewardList);
				mv.addObject("price", price);
			}
		}else{
			List<Map<String,Object>> rewardList = countManager.getSanNongRewardlistPage(page);//其他奖励
			List<Map<String, Object>> count = countManager.SanNongOtherAllCount(page);//其他奖励统计
			String price = null;
			if(count.isEmpty()){
				price = "0.00";
			}else{
				for(int i = 0; i < count.size(); i++){
					if(count.get(i) == null){
						price = "0.00";
					}else{
						price = countDec.format(count.get(i).get("price"));
					}
				}
			}
			mv.addObject("rewardList", rewardList);
			mv.addObject("price", price);
		}
		
		mv.setViewName("farm/count/reward_list");
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	@RequestMapping(value="/getsellAllCountlist")
	public ModelAndView getsellAllCountlist(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData(); 
		page.setPd(pd);
		DecimalFormat countDec = new DecimalFormat("0.00");
		DecimalFormat countNum = new DecimalFormat("0");
		List<Map<String,Object>> sellAllCountList = countManager.sellAllCountlistPage(page);//销售总统计列表
		List<Map<String, Object>> count = countManager.sellAllCount(page);//销售总统计
		String num = null;
		String price = null;
		if(count.isEmpty()){
			num = "0";
			price = "0.00";
		}else{
			for(int i = 0; i < count.size(); i++){
				if(count.get(i) == null){
					num = "0";
					price = "0.00";
				}else{
					num = countNum.format(count.get(i).get("orderNum"));
					price = countDec.format(count.get(i).get("price"));
				}
			}
		}
		mv.addObject("sellAllCountList", sellAllCountList);
		mv.addObject("num", num);
		mv.addObject("price", price);
		mv.setViewName("farm/count/sellAllCount_list");
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
}
